/*
========================================================================
 Name        : ThemeListBoxView.cpp
 Author      : shitou
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include <GameZone.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]

#include "GameZone.hrh"
#include "ThemeListBoxView.h"
#include "ThemeListBox.hrh"
#include "ThemeListBox.h"
// ]]] end generated region [Generated User Includes]
#include "GameZoneAppUi.h"

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CThemeListBoxView::CThemeListBoxView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iThemeListBox = NULL;
	// ]]] end generated region [Generated Contents]
    iClientEngine = NULL;
    iLastFocusIndex = 0;
    iFirstOffY = 0;
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CThemeListBoxView::~CThemeListBoxView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	delete iThemeListBox;
	iThemeListBox = NULL;
	// ]]] end generated region [Generated Contents]
    if (iClientEngine && iClientEngine->IsRunning())
    {
        iClientEngine->CancelTransaction();
    }
    delete iClientEngine;
    iClientEngine = NULL;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CThemeListBoxView
 */
CThemeListBoxView* CThemeListBoxView::NewL()
	{
	CThemeListBoxView* self = CThemeListBoxView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CThemeListBoxView
 */
CThemeListBoxView* CThemeListBoxView::NewLC()
	{
	CThemeListBoxView* self = new ( ELeave ) CThemeListBoxView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CThemeListBoxView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_THEME_LIST_BOX_THEME_LIST_BOX_VIEW );
				
	// ]]] end generated region [Generated Code]
	
	// add your own initialization code here
	StatusPane()->MakeVisible(EFalse);
	}

/**
 * @return The UID for this view
 */
TUid CThemeListBoxView::Id() const
	{
	return TUid::Uid( EThemeListBoxViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CThemeListBoxView::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case EThemeListBoxViewPrePageCommand:
			commandHandled = HandlePrePageSelectedL( aCommand );
			break;
		case EThemeListBoxViewNextPageCommand:
			commandHandled = HandleNextPageSelectedL( aCommand );
			break;
		case EThemeListBoxViewChangeTypeCommand:
			commandHandled = HandleChangeTypeSelectedL( aCommand );
			break;
		case EThemeListBoxViewSearchCommand:
			commandHandled = HandleSearchSelectedL( aCommand );
			break;
		case EThemeListBoxViewAboutCommand:
			commandHandled = HandleAboutSelectedL( aCommand );
			break;
		case EThemeListBoxViewExitCommand:
			commandHandled = HandleExitSelectedL( aCommand );
			break;
		default:
			break;
		}
		
	if ( !commandHandled ) 
		{
	
		if ( aCommand == EAknSoftkeyExit || aCommand == EThemeListBoxViewExitCommand )
			{
			AppUi()->HandleCommandL( EEikCmdExit );
			}
	
		}
	// ]]] end generated region [Generated Code]
	
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CThemeListBoxView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid aCustomMessageId,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
				
	if ( iThemeListBox == NULL )
		{
		iThemeListBox = CreateContainerL();
		iThemeListBox->iParentView=this;
		iThemeListBox->SetOwnerView(this);
		iThemeListBox->SetMopParent( this );
		AppUi()->AddToStackL( *this, iThemeListBox );
		} 
	// ]]] end generated region [Generated Contents]
    if (iClientEngine == NULL)
    {
        iClientEngine = CClientEngine::NewL(iThemeListBox);
        iClientEngine->SetSelectedIap(((CGameZoneAppUi*) (iEikonEnv->AppUi()))->getIap());
    }
	RefashThemeListBox();
	}

/**
 */
void CThemeListBoxView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
    if (iClientEngine && iClientEngine->IsRunning())
    {
        iClientEngine->CancelTransaction();
        iClientEngine->SetObserver(NULL);
    }
    delete iClientEngine;
    iClientEngine = NULL;
	if ( iThemeListBox != NULL )
		{
		iLastFocusIndex = iThemeListBox->ListBox()->CurrentItemIndex();
		iFirstOffY = iThemeListBox->ListBox()->FirstControlOffY();
		AppUi()->RemoveFromViewStack( *this, iThemeListBox );
		delete iThemeListBox;
		iThemeListBox = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CThemeListBoxView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}

// [[[ begin generated function: do not modify
void CThemeListBoxView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_THEME_LIST_BOX_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

// ]]] end generated function

// [[[ begin generated function: do not modify
void CThemeListBoxView::CleanupStatusPane()
	{
	}

// ]]] end generated function

/**
 *	Creates the top-level container for the view.  You may modify this method's
 *	contents and the CThemeListBox::NewL() signature as needed to initialize the
 *	container, but the signature for this method is fixed.
 *	@return new initialized instance of CThemeListBox
 */
CThemeListBox* CThemeListBoxView::CreateContainerL()
	{
	return CThemeListBox::NewL( AppUi()->ApplicationRect(), NULL, this );
	}

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandlePrePageSelectedL( TInt aCommand )
	{
    ((CGameZoneAppUi*) (iEikonEnv->AppUi()))->getPage(EFalse);
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandleNextPageSelectedL( TInt aCommand )
	{
    ((CGameZoneAppUi*) (iEikonEnv->AppUi()))->getPage();
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandleChangeTypeSelectedL( TInt aCommand )
	{
    ((CGameZoneAppUi*) (iEikonEnv->AppUi()))->SelectMobileType();
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandleSearchSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandleAboutSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CThemeListBoxView::HandleExitSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	return EFalse;
	}

void CThemeListBoxView::DynInitMenuPaneL( TInt aResourceId,CEikMenuPane* aMenuPane)
{
    if (R_THEME_LIST_BOX_MENU_PANE1_MENU_PANE == aResourceId)//��������һ��Ҫ����if�ж�
    {
        aMenuPane->SetItemDimmed(EThemeListBoxViewPrePageCommand, !((CGameZoneAppUi*) (iEikonEnv->AppUi()))->HasPrePage());
        aMenuPane->SetItemDimmed(EThemeListBoxViewNextPageCommand, !((CGameZoneAppUi*) (iEikonEnv->AppUi()))->HasNextPage());
        
        aMenuPane->SetItemDimmed(EThemeListBoxViewSearchCommand, ETrue);
        aMenuPane->SetItemDimmed(EThemeListBoxViewAboutCommand, ETrue);
    }
}

void CThemeListBoxView::RefashThemeListBox()
{
	if (((CGameZoneAppUi*) (iEikonEnv->AppUi()))->HasGetListData())
		{
		iLastFocusIndex = 0;
		iFirstOffY = 0;
		((CGameZoneAppUi*) (iEikonEnv->AppUi()))->setHasGetListData(EFalse);
		}
	iThemeListBox->setTitleTextL(((CGameZoneAppUi*) (iEikonEnv->AppUi()))->MobilerTypeName());
	iThemeListBox->setThemeTypes(((CGameZoneAppUi*) (iEikonEnv->AppUi()))->ThemeType());
	iThemeListBox->InitListWithThemeItems(((CGameZoneAppUi*) (iEikonEnv->AppUi()))->ThemeListItem());
	iThemeListBox->ListBox()->SetCurrentItemIndex(iLastFocusIndex);
	iThemeListBox->ListBox()->UpdateControls(iFirstOffY);
}

TInt CThemeListBoxView::GetListItemBitmap(const TDesC8& aUrl, TInt aActionType)
{
    if (iClientEngine && iClientEngine->IsRunning())
    {
        iClientEngine->CancelTransaction();
    }
    TRAPD(err, iClientEngine->IssueHTTPGetL(aUrl,aActionType));
    return err;
}
